package leecode;

/**
 * @author dushougudu
 */
public class RotateArray {
    public static void main(String[] args) {
        int nums[] = new int[]{1, 2, 3, 4, 5, 6, 7};
        rotate2(nums, 3);
        for (int i : nums) {
            System.out.println(i);
        }
    }

    public static void rotate1(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
    }

    public static void rotate2(int[] nums, int k) {
        int newNums[] = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            newNums[(i + k) % nums.length] = nums[i];
        }
        System.arraycopy(newNums, 0, nums, 0, nums.length);
    }

    public static void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start++;
            end--;
        }
    }

    public static int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int pivot = left + (right - left) / 2;
            if (target > nums[pivot]) {
                left = pivot + 1;
            } else if (target < nums[pivot]) {
                right = pivot - 1;
            } else {
                return pivot;
            }
        }

        return -1;
    }
}
